#!/Users/timmenzies/opt/gawk/bin/gawk -f 
 
# readarff.awk
# Revision: 296
# Built by timmenzies on Fri Jul 29 20:43:58 PDT 2011.

#   _ _ _                                               ((
#  |=|=|=|.----------------------------------------.    _))_
#  |-|-|-|| AWK code of a tiny book on data mining | ___\__/)_
#  |_|_|_|'----------------------------------------| |_|_~~_|_|

# This file is part of TINY.
#
# TINY is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TINY is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
#  along with TINY.  If not, see <http://www.gnu.org/licenses/>.

 @include "lib.awk"
 @include "config.awk"

 BEGIN         {  IGNORECASE = 1 }
 /@attribute/  { A++; Name[A]=$2; 
                 $3 ~ /(numeric|integer|real|float)/ ? Num[A] : Sym[A]
               }
 /@data/       { In=1; FS=","; C=0; 
                 if (K) {
                    K = K>0 ? K : K += A + 1
                 } else { K = A }
                 next }
 /@/           { next }
               { gsub(/%.*/,""); gsub(/[ \t'\"]*/,"");  if (/^$/) next }
 In && NF==A   { cells(C,initRow()) }
 END           { if(AfterAllRows) @AfterAllRows(C) }

 BEGIN{_R=0}  
 function initRow(    col) {
     _R++
     for(col=1;col<=A;col++) Data[_R, col] = $col
     return _R
 } # end _R
 function cells(c,row) {
     if(BeforeEachRow) @BeforeEachRow(c,row)
     cells1(c,row)
     if(AfterEachRow)  @AfterEachRow(c,row)
 }
 function cells1(c,row,     klass,col,val,key1,key2) {
     klass = Data[row,K]
     Has[c][row] 
     Klass[c][klass]
	 for(col in Name) {
         key1 = c _ col _ klass # cache keys for faster load times
         key2 = c _ col _ "_"   
         if ((val = Data[row,col]) != Missing)   
			if (col in Sym) {
             	symCell(key1,val)
                symCell(key2,val)
            } else {
                numCell(key1,val,row)
                numCell(key2,val,row)
 }}}
 function symCell(key,val) {
     F[key][val]++
     Fall[key]++
 }
 function numCell(key,val,row) {
     N[key]++
     Sum[key]   += val
     SumSq[key] += val * val
     Value[key][row]=val
     if (key in Max) { if (val > Max[key]) Max[key] = val } else Max[key] = val
     if (key in Min) { if (val < Min[key]) Min[key] = val } else Min[key] = val
 }

 function readArffDump(    i,j,k) {
  print "K=" K
  for(i in Klass) for(j in Klass[i]) print "cluster " i " has klass " j
  o(Name,"name")
  o(Max, "max")
  o(Min, "min")
  o(N,"N")
  o(Sum,"Sum") ; o(SumSq,"SumSq"); 
  for(i in F) 
     for (j in F[i]) {
         k = i; gsub(_,";",k)
         print "F[ " k " ][ " j " ] = " F[i][j] | "sort -k 4"
  }
  o(Fall,"Fall")
  }

